
#ifndef __BETTY_TERMINAL_H__
#define __BETTY_TERMINAL_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

/*--------------------------------------------------------------------------*/
/*									    */
/*	Screen coordinates related types				    */
/*									    */
/*	The screen coordinates [x, y] are in the interval		    */
/*	[0 .. width - 1, 0 .. height - 1]. 				    */
/*									    */
/* 	If you create the terminal with scrollback lines, than the interval */
/*	is extended to the following:					    */
/*	[0 .. width - 1, -scrollback .. 0 .. height - 1].		    */
/*									    */
/*--------------------------------------------------------------------------*/

/* Position at terminal screen or scrollback buffer. The
   origin (0, 0) is at the upper left screen corner */
struct Betty_Position {
        int x;
        int y;
};

/* An rectangual area at the terminal screen.
   Additionally there are aliases to make the access
   more comfortable. */
struct Betty_Rect {
        union {
                struct Betty_Position top_left;         /* top left cell */

                struct {
                        int left;
                        int top;
                };
        };

        union {
                struct Betty_Position bottom_right;     /* bottom right cell */

                struct {
                        int right;
                        int bottom;
                };
        };
};

/*--------------------------------------------------------------------------*/
/*	Types for screen model (content) access 			    */
/*--------------------------------------------------------------------------*/

/* forward declaration of the terminal type */
struct Betty_Terminal;

/* currently we use UCS-2. */
typedef uint16_t Betty_Character;


/* Currently supported color palettes. */
#define BETTY_PALETTE_DEFAULT	0		/* default terminal colors */
#define BETTY_PALETTE_XTERM	1		/* xterm colors */

/* Colors from the default palette */
#define BETTY_COLOR_DEFAULT_FG	0		/* default \033[m fg color */
#define BETTY_COLOR_DEFAULT_BG	1		/* default \033[m bg color */


struct Betty_CellAttribute {
        unsigned char   fg_color_idx;		/* foreground color index */
        unsigned char   bg_color_idx;		/* background color index */

	unsigned char	fg_palette	: 1;
	unsigned char	bg_palette	: 1;

	/* graphic rendition flags */
        unsigned char   underline	: 1;
	unsigned char	reverse		: 1;
	unsigned char	blink		: 1;
	unsigned char	bold		: 1;
};


struct Betty_LineAttribute {
};

struct Betty_Cell {
        Betty_Character                 uc;     /* unicode of glyph */
        struct Betty_CellAttribute      attr;
};

struct Betty_Line {
/*         struct Betty_LineAttribute      attr; */
        struct Betty_Cell               *cell;
        int                    		cell_count;
};

/*--------------------------------------------------------------------------*/
/*									    */
/*	Callback related types						    */
/*									    */
/*	The libbetty is the middle part in a terminal emulator application: */
/*									    */
/*	+----------------+        +----------+        +----------------+    */
/*	| I/O (pty etc.) |  <-->  | libbetty |  <-->  | user interface |    */
/*	+----------------+        +----------+        +----------------+    */
/*									    */
/*--------------------------------------------------------------------------*/

/*
 * User interface functions that are called from this
 * library.
 */
struct Betty_UiOperations {
        void (* scrollUp)       (void *ui, int top, int bottom, int count);
        void (* scrollDown)     (void *ui, int top, int bottom, int count);
        void (* drawCell)       (void *ui, struct Betty_Position pos, 
                                        const struct Betty_Cell *cell);
        void (* drawRect)       (void *ui, struct Betty_Rect rect);
        void (* moveCursor)     (void *ui, struct Betty_Position pos);

        void (* bell)           (void *ui);

	void (* resizeScreen)	(void *ui, int width, int height);
};

/*
 * Function type of transmit function, that is used to transmit
 * terminal responses or key codes.
 */
typedef int (* Betty_TransmitFunction)(void *xmit, const void *buf,
					unsigned int count);


/*--------------------------------------------------------------------------*/
/*	API constants and Functions					    */
/*--------------------------------------------------------------------------*/

/* default cell attribute, if no graphic rendition is set */
extern const struct Betty_CellAttribute BETTY_DEFAULT_CELL_ATTR;


/* create/destroy */
struct Betty_Terminal * Betty_CreateTerminal (int width, int height, int scrollback);
void Betty_DestroyTerminal (struct Betty_Terminal *term);
int Betty_Resize (struct Betty_Terminal *term, int width, int height, 
			int scrollback);

/* callback stuff */
void Betty_SetUi (struct Betty_Terminal *term, void *ui, 
                        const struct Betty_UiOperations *ops);
void Betty_SetTransmitFunction (struct Betty_Terminal *term, void *xmit,
			Betty_TransmitFunction func);

/* screen model related functions */
int Betty_GetWidth (struct Betty_Terminal *term);
int Betty_GetHeight (struct Betty_Terminal *term);
int Betty_GetScrollBack (struct Betty_Terminal *term);
const struct Betty_Line * Betty_GetLine (struct Betty_Terminal *term, int y);
const struct Betty_Position * Betty_GetCursorPosition (struct Betty_Terminal *term);

/* I/O */
int Betty_ReceiveDataUTF8 (struct Betty_Terminal *term, const unsigned char *buf, 
				unsigned int count);
int Betty_SendData (struct Betty_Terminal *term, const void *buf,
			unsigned int count);
int Betty_SendString (struct Betty_Terminal *term, const char *format, ...);

#ifdef __cplusplus
}
#endif

#endif


